//
//  LTQTabBarController.m
//  UI07_homeWork(halfOfSugar)
//
//  Created by Hanlingyu on 16/7/23.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "LTQTabBarController.h"
#import "HomePageViewController.h"
#import "ArticleViewController.h"
#import "MyselfViewController.h"
#import "FoundViewController.h"
#import "MessageViewController.h"
#import "ViewController.h"
#import "LoginViewController.h"
BOOL isLogin;
@interface LTQTabBarController ()


@property (nonatomic, strong) UIView *backgroundView;



@end


@implementation LTQTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    isLogin = NO;
    
    
    [self loadViewController];
    
    [self createCustomerTabBar];
    
    
    
    
    
}

- (void)loadViewController {
    
    
    
    for (UIView *subView in self.tabBar.subviews) {
        [subView removeFromSuperview];
    }
    self.tabBar.hidden = YES;
    
    

    
}

- (void)createCustomerTabBar {
    
    self.backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 49, self.view.frame.size.width, 49)];
    _backgroundView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_backgroundView];

    NSArray *normalImage = @[@"tab_首页", @"tab_社区", @"tab_publish_add", @"tab_分类", @"tab_我的"];
    NSArray *lightImage = @[@"tab_首页_pressed", @"tab_社区_pressed", @"tab_publish_add", @"tab_分类_pressed", @"tab_我的"];
    
    
    CGFloat buttonWidth = ([UIScreen mainScreen].bounds.size.width - 40) / 5;
    for (int i = 0; i < 5; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor clearColor];
        [button setImage:[UIImage imageNamed:normalImage[i]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:lightImage[i]] forState:UIControlStateSelected];
        button.frame = CGRectMake(i * (buttonWidth + 10), 0, buttonWidth, 49);
        [self.backgroundView addSubview:button];
        button.tag = i + 1000;
        
        if (i == 0) {
            
            button.selected = YES;
        }
        
        if ((i == 3 || i == 4) && isLogin == NO) {
            [button addTarget:self action:@selector(loginView:) forControlEvents:UIControlEventTouchUpInside];
        } else if (i == 2){
            [button addTarget:self action:@selector(writeView:) forControlEvents:UIControlEventTouchUpInside];
        } else {
            [button addTarget:self action:@selector(clickedAction:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}

- (void)loginView:(UIButton *)button {
   
    [self presentViewController:[[LoginViewController alloc] init] animated:YES completion:^{
    }];
}

- (void)writeView:(UIButton *)button {
    [self presentViewController:[[ViewController alloc] init] animated:YES completion:^{
    }];

}


- (void)clickedAction:(UIButton *)button {
    
    for (UIButton *otherButton in self.backgroundView.subviews) {
        
        otherButton.selected = NO;
    }
    button.selected = !button.selected;
    self.selectedIndex = button.tag - 1000;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
